package com.qijian.flowable.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qijian.common.utils.DateUtils;
import com.qijian.common.utils.ObjectUtils;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.common.utils.poi.ExcelUtil;
import com.qijian.flowable.domain.Matrix;
import com.qijian.flowable.domain.MatrixField;
import com.qijian.flowable.domain.query.MatrixFieldQuery;
import com.qijian.flowable.domain.query.MatrixFieldValQuery;
import com.qijian.flowable.domain.vo.MatrixFieldVo;
import com.qijian.flowable.mapper.MatrixFieldMapper;
import com.qijian.flowable.mapper.MatrixMapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.qijian.flowable.mapper.MatrixFieldValMapper;
import com.qijian.flowable.domain.MatrixFieldVal;
import com.qijian.flowable.service.IMatrixFieldValService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 流程矩阵字段值Service业务层处理
 *
 * @author qijian
 * @date 2022-02-08
 */
@Service
@RequiredArgsConstructor
public class MatrixFieldValServiceImpl extends ServiceImpl<MatrixFieldValMapper, MatrixFieldVal> implements IMatrixFieldValService {

    private final MatrixFieldValMapper matrixFieldValMapper;

    private final MatrixFieldMapper matrixFieldMapper;

    private final MatrixMapper matrixMapper;


    @Override
    @Transactional
    public void saveOrUpdateMatrixFieldVal(List<MatrixFieldVal> matrixFieldValList) {

        matrixFieldValMapper.deleteByMatrixId(matrixFieldValList.get(0).getMatrixId());

        matrixFieldValList.forEach(x -> {
            matrixFieldValMapper.insert(x);
        });
    }

    @Override
    public List<MatrixFieldVo> export(MatrixFieldValQuery query) {

        MatrixFieldQuery matrixFieldQuery = new MatrixFieldQuery();
        matrixFieldQuery.setMatrixId(query.getMatrixId());
        List<MatrixField> matrixFieldList = matrixFieldMapper.selectList(WhereEntityTool.invoke(matrixFieldQuery));
        List<MatrixFieldVal> matrixFieldValList = matrixFieldValMapper.selectList(WhereEntityTool.invoke(query));
        List<MatrixFieldVo> matrixFieldVos = new ArrayList<>();
        matrixFieldList.forEach(x -> {
            List<MatrixFieldVal> list = matrixFieldValList.stream().filter(val -> val.getLabel().equals(x.getFieldName())).collect(Collectors.toList());
            list.forEach(val -> {
                Matrix matrix =matrixMapper.selectById(val.getMatrixId());
                MatrixFieldVo vo = new MatrixFieldVo();
                vo.setValueName(val.getValueName());
                vo.setCreateBy(val.getCreateBy());
                vo.setName(x.getName());
                vo.setLabel(x.getLabel());
                vo.setType(val.getType());
                if (matrix != null) {
                    vo.setMatrixName(matrix.getName());
                    vo.setMatrixId(matrix.getMatrixId());
                }
                matrixFieldVos.add(vo);
            });
        });
        return matrixFieldVos;
    }

}
